package com.mdhw.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdhw.admin.dao.AdminMenuDao;
import com.mdhw.admin.entity.AdminMenu;
import com.mdhw.admin.entity.AdminRoleMenu;
import com.mdhw.admin.service.AdminMenuService;
import com.mdhw.admin.service.AdminRoleMenuService;
import com.mdhw.admin.vo.AdminMenuTree;
import com.mdhw.admin.vo.AdminMenuVo;
import com.mdhw.common.enums.StatusEnum;
import com.mdhw.config.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【admin_menu】的数据库操作Service实现
* @createDate 2024-12-04 13:43:21
*/
@Service
public class AdminMenuServiceImpl extends ServiceImpl<AdminMenuDao, AdminMenu> implements AdminMenuService{

    @Autowired
    private AdminMenuDao adminMenuDao;
    @Autowired
    private AdminRoleMenuService adminRoleMenuService;
    @Override
    public List<AdminMenuTree> selectMenuTree(Integer filterType, List<Long> checkedIdList) {
        // 查询所有菜单
        QueryWrapper<AdminMenuTree> smLqw = new QueryWrapper<>();
        smLqw.eq("status", StatusEnum.OK.getCode().toString());
        if(filterType != null){
            smLqw.ne("type",filterType);
        }
        List<AdminMenuTree> sysMenuList = this.adminMenuDao.selectAdminMenuTreeList(smLqw);
        List<AdminMenuTree> parent0SysMenuTreeList = new ArrayList<>();

        for(AdminMenuTree sysMenuTree : sysMenuList){
            Long menuId = sysMenuTree.getId();

            if(checkedIdList.contains(menuId)){
                sysMenuTree.setChecked(Boolean.TRUE);
            }else{
                sysMenuTree.setChecked(Boolean.FALSE);
            }
            if(sysMenuTree.getParentId() == 0){
                parent0SysMenuTreeList.add(sysMenuTree);
            }
        }


        for(AdminMenuTree sysMenuTree:parent0SysMenuTreeList){
            List<AdminMenuTree> child = getChild(sysMenuTree.getId(), sysMenuList);
            sysMenuTree.setChildren(child);
        }
        return parent0SysMenuTreeList;
    }


    private List<AdminMenuTree> getChild(Long parentId, List<AdminMenuTree> sysMenuList){
        // 子菜单列表
        List<AdminMenuTree> childList = new ArrayList<>();
        for(AdminMenuTree sysMenuTree:sysMenuList){
            if(sysMenuTree.getParentId().equals(parentId)){
                childList.add(sysMenuTree);
            }
        }

        // 遍历 获取子菜单的子菜单
        for (AdminMenuTree menu : childList) {
            List<AdminMenuTree> child = getChild(menu.getId(), sysMenuList);
            menu.setChildren(child);
        }
        //递归出口 childList长度为0
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

    /**
     * 新增菜单
     * @param adminMenuVo
     * @return
     */
    @Override
    public R<String> addAdminMenu(AdminMenuVo adminMenuVo) {
        LambdaQueryWrapper<AdminMenu> smLqw = new LambdaQueryWrapper<>();
        smLqw.eq(AdminMenu::getStatus,StatusEnum.OK.getCode());
        smLqw.eq(AdminMenu::getCode,adminMenuVo.getCode());
        List<AdminMenu> sysMenuList = this.list(smLqw);
        if(sysMenuList != null && sysMenuList.size() > 0){
            return R.error("新增失败! code重复!");
        }
        Date nowTime = new Date();


        AdminMenu adminMenu = new AdminMenu();
        BeanUtils.copyProperties(adminMenuVo,adminMenu);
        adminMenu.setCreateDate(nowTime);
        adminMenu.setUpdateDate(nowTime);
        adminMenu.setStatus(StatusEnum.OK.getCode().toString());
        adminMenu.setRevision(0);
        adminMenu.setSortNum(0);

        if(adminMenuVo.getParentId() == null){
            adminMenu.setParentId(0L);
        }
        boolean isSuccess = this.save(adminMenu);
        if(isSuccess){
            Long adminMenuId = adminMenu.getId();
            AdminRoleMenu adminRoleMenu = new AdminRoleMenu();
            adminRoleMenu.setMenuId(adminMenuId);
            adminRoleMenu.setRoleId(1L);
            this.adminRoleMenuService.save(adminRoleMenu);
            return R.success("新增成功");
        }else{
            return R.error("新增失败");
        }
    }

    @Override
    public R<String> updateAdminMenu(AdminMenuVo adminMenuVo) {
        String code = adminMenuVo.getCode();
        Long id = adminMenuVo.getId();
        LambdaQueryWrapper<AdminMenu> smLqw = new LambdaQueryWrapper<>();
        smLqw.eq(AdminMenu::getStatus,StatusEnum.OK.getCode());
        smLqw.eq(AdminMenu::getCode,code);
        smLqw.ne(AdminMenu::getId,id);
        List<AdminMenu> sysMenuList = this.list(smLqw);
        if(sysMenuList != null && sysMenuList.size() > 0){
            return R.error("修改失败! code重复!");
        }else{
            AdminMenu nativeSysMenu = this.getById(id);

            AdminMenu sysMenu = new AdminMenu();
            BeanUtils.copyProperties(adminMenuVo,sysMenu);
            sysMenu.setCreateUser(nativeSysMenu.getCreateUser());
            sysMenu.setCreateDate(nativeSysMenu.getCreateDate());
            sysMenu.setStatus(nativeSysMenu.getStatus());
            sysMenu.setSortNum(nativeSysMenu.getSortNum());
            sysMenu.setRevision(nativeSysMenu.getRevision());
            boolean isSuccess = this.updateById(sysMenu);
            if(isSuccess){
                return R.success("修改成功");
            }else{
                return R.error("修改失败");
            }
        }
    }

    @Override
    public R<String> deleteAdminMenu(Long id) {
        AdminMenu sysMenu = this.getById(id);
        sysMenu.setStatus(StatusEnum.DELETE.getCode());
        boolean isSuccess = this.updateById(sysMenu);
        if(isSuccess){
            LambdaQueryWrapper<AdminRoleMenu> srmLqw = new LambdaQueryWrapper<>();
            srmLqw.eq(AdminRoleMenu::getMenuId,id);
            this.adminRoleMenuService.remove(srmLqw);
        }
        if(isSuccess){
            return R.success("删除成功!");
        }else{
            return R.error("删除失败!");
        }
    }
}




